package com.ele.Servlet;



import com.ele.Pojo.Food;
import com.ele.Pojo.PageResult;
import com.ele.Pojo.vo.FoodVO;
import com.ele.Service.FoodService;
import com.ele.Service.impl.FoodServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.List;

@WebServlet("/food")
public class FoodServlet extends HttpServlet {
    private FoodService foodService = new FoodServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        switch (op){
            case "add": addFood(req,resp);break;
            case "update": updateFood(req,resp);break;
            case "delete": deleteFood(req,resp);break;
            case "find": findFood(req,resp);break;
        }
    }

    private void findFood(HttpServletRequest req, HttpServletResponse resp) throws  IOException{
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String categoryId = req.getParameter("categoryId");
        String shopId = req.getParameter("shopId");
        String foodName = req.getParameter("foodName");

        PageResult<List<FoodVO>> pageResult = this.foodService.findFood(currentPage,pageSize,categoryId,shopId,foodName);
        String json = new Gson().toJson(pageResult);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    private void deleteFood(HttpServletRequest req, HttpServletResponse resp)throws  IOException {
        String foodId = req.getParameter("foodId");
        Integer x = this.foodService.deleteFood(foodId);
        resp.getWriter().write(""+x);
    }

    private void updateFood(HttpServletRequest req, HttpServletResponse resp) throws  IOException{
        String foodId = req.getParameter("foodId");
        String foodName = req.getParameter("foodName");
        String foodExplain = req.getParameter("foodExplain");
        String foodImg = req.getParameter("foodImg");
        String foodPrice = req.getParameter("foodPrice");
        String shopId = req.getParameter("shopId");
        String remarks = req.getParameter("remarks");
        String status = req.getParameter("status");
        String stock = req.getParameter("stock");
        String categoryId = req.getParameter("categoryId");
        String recommend = req.getParameter("recommend");
        String hot = req.getParameter("hot");
        Food food = new Food();
        food.setFoodId(Integer.valueOf(foodId));
        food.setFoodName(foodName);
        food.setFoodExplain(foodExplain);
        food.setFoodImg(foodImg);
        food.setFoodPrice(Double.valueOf(foodPrice));
        food.setShopId(Integer.valueOf(shopId));
        food.setRemarks(remarks);
        food.setStatus(Integer.valueOf(status));
        food.setStock(Integer.valueOf(stock));
        food.setCategoryId(Integer.valueOf(categoryId));
        food.setRecommend(Integer.valueOf(recommend));
        food.setHot(Integer.valueOf(hot));
        Integer x = this.foodService.updateFood(food);
        resp.getWriter().write(""+x);

    }

    private void addFood(HttpServletRequest req, HttpServletResponse resp) throws  IOException{
        String foodName = req.getParameter("foodName");
        String foodPrice = req.getParameter("foodPrice");
        String stock = req.getParameter("stock");
        String remarks = req.getParameter("remarks");
        String foodExplain = req.getParameter("foodExplain");
        String foodImg = req.getParameter("foodImg");
        String shopId = req.getParameter("shopId");
        String categoryId = req.getParameter("categoryId");
        Food food = new Food();
        food.setFoodPrice(Double.valueOf(foodPrice));
        food.setFoodName(foodName);
        food.setFoodExplain(foodExplain);
        food.setFoodImg(foodImg);
        food.setStock(Integer.valueOf(stock));
        food.setRemarks(remarks);
        food.setShopId(Integer.valueOf(shopId));
        food.setCategoryId(Integer.valueOf(categoryId));
        Integer x = this.foodService.addFood(food);
        resp.getWriter().write(""+x);
    }
}
